package com.atguigu.srb.core.controller.api;


import com.atguigu.guigu.common.result.R;
import com.atguigu.service.base.utils.Amount1Helper;
import com.atguigu.service.base.utils.JwtUtils;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.service.TransFlowService;
import com.atguigu.srb.core.service.UserAccountService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/api/core/userAccount")
public class ApiUserAccountController {
    /*
        1、幂等性
        2、还款
        3、个人资金记录
        4、mq:
              削峰填谷  异步解耦合
        5、项目总结
     */
    @Resource
    UserAccountService userAccountService;
    @Resource
    TransFlowService transFlowService;

    @ApiOperation("查询资金记录")
    @GetMapping("tranflows")
    public R tranflows(@RequestHeader(value = "token" ,required = false)
                                   String token){
        List<TransFlow> transFlows = transFlowService.tranflows(token);
        return R.ok().data("items" , transFlows);
    }


    ///api/core/userAccount/notifyWithdraw
    @ApiOperation("提现回调接口")
    @PostMapping("notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request){
        return userAccountService.notifyWithdraw(request);
    }
    @ApiOperation("提现")
    @GetMapping("/auth/withdraw/{money}")
    public R withDraw(@PathVariable("money")BigDecimal money ,
                      @RequestHeader(value = "token",required = false)String token){
        String formStr = userAccountService.withDraw(money,token);

        return R.ok().data("formStr",formStr);
    }


    /*
        投资业务：
           1、校验用户账户是否正常
           2、校验用户账户余额是否足够
           3、校验标的状态
                是否是募资中
                标的是否已满标
           4、创建标的投资项保存到数据库
           5、冻结账户投标金额
                需要和hfb交互
           6、保存账户交易流水
     */


    @ApiOperation("计算投资收益")
    @GetMapping("/getInterestCount/{invest}/{period}/{yearRate}/{returnMethod}")
    public R getInterestCount(
            @ApiParam(value = "投资金额") @PathVariable("invest")BigDecimal invest,
            @ApiParam(value = "投资期数") @PathVariable("period")Integer period,
            @ApiParam(value = "年化率") @PathVariable("yearRate")BigDecimal yearRate,
            @ApiParam(value = "还款方式") @PathVariable("returnMethod")Integer returnMethod){
        /*
            收益：
                投资金额
                投资期数
                年化率
                还款方式
         */
        BigDecimal bigDecimal = userAccountService.getInterestCount(invest,period,yearRate,returnMethod);
        return R.ok().data("interest" , bigDecimal);
    }
    ///api/core/userAccount/notify
    @ApiOperation("充值成功的回调接口")
    @PostMapping("/notify")
    public String chargeNotify(HttpServletRequest request){
        String result = userAccountService.chargeNotify(request);
        return result;
    }

    @ApiOperation("充值")
    @PostMapping("auth/charge/{amount}")
    public R charge(@RequestHeader(value = "token" ,required = false)
                                String token,
                    @PathVariable("amount")Double amount){
        String formStr = userAccountService.charge(token,amount);
        return R.ok().data("formStr",formStr);
    }

    @ApiOperation("查询账户余额")
    @GetMapping("auth/amount")
    public R getAmount(@RequestHeader(value = "token" ,required = false)
                       String token){
        Long userId = JwtUtils.getUserId(token);
        UserAccount userAccount = userAccountService.getOne(Wrappers.lambdaQuery(UserAccount.class)
                .eq(UserAccount::getUserId, userId));
        return R.ok().data("amount" , userAccount==null?
                new BigDecimal("0"):userAccount.getAmount());
    }
}

